import api from './request'

// 类型定义
interface Post {
  id: number
  title: string
  content: string
  authorId: number
  authorNickname?: string
  authorAvatar?: string
  categoryId: number
  status: number
  likeCount: number
  commentCount: number
  createTime: string
  updateTime: string
}

interface Category {
  id: number
  name: string
  description?: string
  icon?: string
  sort: number
  status: number
  createdAt: string
  updatedAt: string
}

// 获取帖子分页列表（带 userType、page、size、categoryId）
export const getPosts = (params: {
  page?: number
  size?: number
  userType?: number
  categoryId?: number
}) => {
  // 前端页码从1开始，后端从0开始，这里自动减1
  const realParams = { ...params, page: params.page ? params.page - 1 : 0 }
  return api.get('/posts', { params: realParams })
}

// 获取帖子详情
export const getPostDetail = (id: number) => {
  return api.get(`/posts/${id}`)
}

// 根据ID获取帖子详情
export const getPostById = (id: number) => {
  return api.get(`/posts/${id}`)
}

// 发布帖子
export const createPost = (data: {
  title: string
  content: string
  categoryId: number
  authorId: number
}) => {
  return api.post<{ postId: number }>('/posts', data)
}

// 更新帖子（新版，PUT /posts，body为PostUpdateDTO）
export const updatePost = (postUpdateDTO: {
  id: number
  title?: string
  content?: string
  status?: number
  categoryId?: number
}) => {
  return api.put('/posts', postUpdateDTO)
}

// 删除帖子
export const deletePost = (id: number) => {
  return api.delete<null>(`/posts/${id}`)
}

// 点赞/取消点赞帖子
export const likePost = (id: number) => {
  return api.post<null>(`/posts/${id}/like`)
}

// 获取用户的帖子列表
export const getUserPosts = (userId: number, params?: {
  page?: number
  size?: number
  sort?: string
}) => {
  return api.get<{
    total: number
    list: Post[]
    pages: number
  }>(`/posts/user/${userId}`, { params })
}

// 获取当前登录用户的所有帖子
export const getMyAllPosts = (authorId: number) => {
  return api.get('/posts/my/all', { params: { authorId } })
}

// ==================== 评论管理 ====================

// 获取评论列表
export const getComments = (postId: number) => {
  return api.get<{
    total: number
    list: Array<{
      id: number
      content: string
      userId: number
      nickname: string
      avatar: string
      likeCount: number
      isLiked: boolean
      createdAt: string
      replies?: Array<{
        id: number
        content: string
        userId: number
        nickname: string
        avatar: string
        likeCount: number
        isLiked: boolean
        createdAt: string
      }>
    }>
  }>(`/comments/post/${postId}`)
}

// 发表评论
export const createComment = (data: {
  postId: number
  content: string
  parentId?: number | null
}) => {
  return api.post<{ commentId: number }>('/comments', data)
}

// 删除评论
export const deleteComment = (id: number) => {
  return api.delete<null>(`/comments/${id}`)
}

// 点赞/取消点赞评论
export const likeComment = (id: number) => {
  return api.post<null>(`/comments/${id}/like`)
}

// 获取分类列表
export const getCategories = (params?: { status?: number }) => {
  return api.get<{
    total: number
    list: Category[]
  }>('/categories', { params })
}

// 获取分类详情
export const getCategoryDetail = (id: number) => {
  return api.get(`/categories/${id}`)
}

// 创建分类（管理员权限）
export const createCategory = (data: {
  name: string
  description: string
  icon: string
  sort: number
  status: number
}) => {
  return api.post<{ categoryId: number }>('/categories', data)
}

// 更新分类（管理员权限）
export const updateCategory = (id: number, data: {
  name: string
  description: string
  icon: string
  sort: number
  status: number
}) => {
  return api.put<null>(`/categories/${id}`, data)
}

// 删除分类（管理员权限）
export const deleteCategory = (id: number) => {
  return api.delete<null>(`/categories/${id}`)
}
